package com.fs.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fs.common.core.pojo.SysRole;
import com.fs.system.mapper.SysRoleMapper;
import com.fs.system.service.ISysRoleService;
import com.fs.system.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysRoleServiceImpl implements ISysRoleService {
    @Autowired
    SysRoleMapper roleMapper;


    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        //查询用户拥有的角色
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        //查询用户所在部门拥有的角色
        List<SysRole> roles2 = roleMapper.selectRolePermissionByDeptId(SecurityUtils.getDeptId());
        Set<String> permsSet = new HashSet<>();
        if(CollectionUtil.isNotEmpty(perms)){
            for (SysRole perm : perms) {
                if (Objects.nonNull(perm)) {
                    permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
                }
            }

        }
        if(CollectionUtil.isNotEmpty(roles2)){
            for (SysRole perm : roles2) {
                if (Objects.nonNull(perm)) {
                    permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
                }
            }
        }
        return permsSet;
    }
}
